package com.mk.carcassonne;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	
	private SoundPool _soundPool;
	private Context _context;
	private HashMap<Integer, Integer> _sounds;
	private AudioManager _audioManager;
	
	public SoundManager(Context context)
	{
		_context = context;
	    _soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
	    _audioManager = (AudioManager)_context.getSystemService(Context.AUDIO_SERVICE);
	    
	    _sounds = new HashMap<Integer, Integer>();
	    _sounds.put(R.raw.knock, _soundPool.load(_context, R.raw.knock, 1));
	    _sounds.put(R.raw.plopp, _soundPool.load(_context, R.raw.plopp, 1));
	    _sounds.put(R.raw.drop, _soundPool.load(_context, R.raw.drop, 1));
	    _sounds.put(R.raw.shwoop, _soundPool.load(_context, R.raw.shwoop, 1));
	}
	
	public void playSound(int resourceId)
	{
		playSound(resourceId, false);
	}
	
	public void playSound(int resourceId, boolean loop)
	{
		float streamVolume = _audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume / _audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    _soundPool.play(_sounds.get(resourceId), streamVolume, streamVolume, 1, loop ? -1 : 0, 1f);
	}
}
